package com.dating.base.service;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.dating.base.bean.Permission;
import com.dating.base.bean.RolePermission;
import com.dating.base.dao.RolePermissionDao;


@IocBean
public class RolePermissionService {
	
	@Inject
	private Dao dao ;
	
	@Inject 
	private RolePermissionDao rolePermissionDao ;
	
	
	/**
	 * 根据角色id获取拥有的权限
	 * @param rid
	 * @return
	 */
	public List<Permission> getPermissions(int rid){
		return rolePermissionDao.getPermissions(rid);
	}
	
	/**
	 * 更新用户权限
	 * @param userPer
	 */
	public void save(RolePermission rolePer){
		Cnd cnd = Cnd.where("roleId","=", rolePer.getRoleId());
		cnd.and("permissionId", "=", rolePer.getPermissionId());
		RolePermission rp = rolePermissionDao.findByCondition(cnd);
		if(rp == null){
			rolePermissionDao.save(rolePer);
		}else{
			rolePer.setId(rp.getId());
			rolePermissionDao.update(rolePer);
		}
	}
	
	
	/**
	 * 
	 * @param userPer
	 * @param status
	 */
	public void setPermission(RolePermission rolePer ,String status){
		if("Y".equals(status)){
			save(rolePer);
		}
		if("N".equals(status)){
			Cnd cnd = Cnd.where("roleId","=", rolePer.getRoleId());
			cnd.and("permissionId", "=", rolePer.getPermissionId());
			dao.clear(RolePermission.class, cnd);
		}
	}
	
}
